package lion.blog.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.lang.StringUtils;

import lion.blog.model.BlogArticle;
import lion.blog.service.BlogService;
import lion.blog.util.BlogUtil;
import lion.dev.lang.MapJ;
import lion.framework.core.bean.BeanFactory;
import lion.web.platform.system.prop.PropService;
import lion.web.platform.system.prop.model.ModelProp;
import lombok.Setter;

/**
 * 获取某篇文章
 * 
 * @author lion
 * @email hl_0074@sina.com
 * @date 2015年9月21日
 */
public class ArticleTag extends BodyTagSupport {

	private static final long serialVersionUID = -3443939708318784585L;

	@Setter
	private int id;
	@Setter
	private String alias;
	@Setter
	private String var = "article";

	@Override
	public int doStartTag() throws JspException {

		// load the blog
		BlogService service = BeanFactory.getBeanFactory(BeanFactory.PLATFORM_BEAN_FACTORY).get("blog.BlogService");
		PropService propService = BeanFactory.getBeanFactory(BeanFactory.PLATFORM_BEAN_FACTORY).get("platform.PropService");
		BlogArticle article = null;
		if (this.id > 0) {
			article = service.getBlogArticle(this.id);
		} else if (StringUtils.isNotBlank(this.alias)) {
			article = service.getBlogArticle(this.alias);
		}

		if (article == null) { return SKIP_BODY; }
		MapJ map = article.valueMap();
		map.put("url", BlogUtil.getBlogLink(map, null));
		map.put("catelog", service.getBlogCatelog(article.getCatelog()));
		for (ModelProp mp : propService.getModelProps("blog.article", article.getId())) {
			map.put(mp.getProp(), mp.getValue());
		}
		pageContext.setAttribute(var, map);

		return EVAL_BODY_INCLUDE;
	}

	@Override
	public void release() {

		pageContext.removeAttribute(var);
		super.release();
	}
}
